<?php
/**
 * Array Helper
 *
 * @package		CodeIgniter
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Tisa Pathumwan
 * 
 * 
 * 
 * 
 * 
 */

// ------------------------------------------------------------------------

function clear_tags($str=""){
	$str = str_replace("<p>", "", $str);
	$str = str_replace("</p>", "", $str);

	return $str;
}

function get_text_pad($txt,$char='0',$length=5){
	return str_pad($txt, $length, $char, STR_PAD_LEFT);
}

/* **************************************************************** */
/* 	DESCRIPTION: 
/*		This function supports only maximum 2-dimensional array.
/*		Recommend input and output array to be same dimensions.
/*		
/*	CREATED: Thida D. / 14 Jan 2010
/*
/* 	INPUT:
/*		$php_array 			- PHP Array (Please query only columns 
/*											you need in javascript array)
/*		$js_array_name	- variable name of javascript array
/*		$dimension			- number of dimension to be output (default = 2)
/*
/*	OUTPUT:
/*		$js_code				- string of javascript code
/*	
/*  Example of Input 1-dimensional PHP array
/*		array (
/*				[0] => 'AAA'
/*				[1] => 'BBB'
/*			)
/*
/*  Example of Output 1-dimensional array
/*			$js_array_name[0] => 'AAA'
/*			$js_array_name[1] => 'BBB'
/*
/*
/*  Example of Input 2-dimensional PHP array
/*		array (
/*				[0] => array (
/*								[aid] => 1
/*								[name] => 'AAA'
/*					)	
/*				[1] => array (
/*								[aid] => 2
/*								[name] => 'BBB'
/*					)	
/*			)
/*
/*  Example of Output 2-dimensional array
/*			$js_array_name[0]['aid'] => '1'
/*			$js_array_name[0]['name'] => 'AAA'
/*			$js_array_name[1]['aid'] => '2'
/*			$js_array_name[1]['name'] => 'BBB'
/* **************************************************************** */
function makeJavaScriptArray( $php_array, $js_array_name, $dimension='2' ) 
{
	$js_code = "";
	$x = 0;
	if (count(@$php_array) > 0 && !empty($php_array))
	{
		$js_code = "var ".$js_array_name." = new Array (".count($php_array)."); \n";
		foreach ($php_array as $k=>$v)
		{
			if (is_array($v))
			{
				if ($dimension == '2') $js_code .= $js_array_name."[".$k."] = new Array (".count($v)."); \n";
				foreach ($v as $fk=>$fv)
				{
					if ($dimension == '2')
						$js_code .= $js_array_name."[".$x."]['".$fk."'] = '".$v[$fk]."'; \n";
					else
						$js_code .= $js_array_name."['".$x."'] = '".$v[$fk]."'; \n";
				}
			}
			else
			{
				$js_code .= $js_array_name."['".$k."'] = '".$php_array[$k]."'; \n";
			}
			$x++;
		}
	}
	return $js_code;
}

?>